<?php

include_once("parser.php");

class forchanParser  extends Parser{
	function __construct() {
		parent::__construct();
		$this->url = "http://boards.4chan.org/v/";
		$this->name = "4chan";
	}
	
	function scrapePosts() {
		$posts = array();
		try {
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $this->url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$html = curl_exec($ch);      
			curl_close($ch);
			
			$dom = new domDocument;
			@$dom->loadHTML($html);
			$dom->preserveWhiteSpace = true;
			$finder = new DomXPath($dom);
			$classname = " thing id-t3";
			
			$threads = $finder->query("//div[@class='board']/div[@class='thread']/div[@class='postContainer opContainer']/div[@class = 'post op']/blockquote");
			$images = $finder->query("//div[@class='board']/div[@class='thread']/div[@class='postContainer opContainer']/div[@class = 'post op']/div[@class = 'file']/a[@class = 'fileThumb']");
			$replys = $finder->query("//div[@class='board']/div[@class='thread']/div[@class='postContainer opContainer']/div[@class = 'post op']/div[@class = 'postInfo desktop']/span[@class='postNum']/span/a");
			$nodeNo = $threads->length;
			
			for($i=0; $i<$nodeNo; $i++) {
				$post = new Post();
				$post->rank = ($i+1);
				//echo $threads->item($i)->nodeValue . '<br>';
				$post->content = $threads->item($i)->nodeValue;
				//echo "<img src=".$images->item($i)->getAttribute('href') . '><br>';
				$post->image = $images->item($i)->getAttribute('href');
				//echo "http://boards.4chan.org/v/".$replys->item($i)->getAttribute('href') . '<br>';
				$post->url = "http://boards.4chan.org/v/".$replys->item($i)->getAttribute('href') ;
				$node = $threads->item($i);
				
				$post->parent = $this->name;
				array_push($posts, $post);
			}
		}
		catch (Exception $e) {
				
		}
		return $posts;
	}
}

?>